package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.SaleCountVo;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerReturnListServiceImpl implements CustomerReturnListService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CustomerReturnListDao customerReturnListDao;
    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;

    //2.1、客户退货单保存  并保存对应退货商品
    @Override
    @Transactional
    public ServiceVO saveCustomerReturnListAndGoods(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, String returnNumber) {
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        Gson gson = new Gson();
        List<CustomerReturnListGoods> customerReturnListGoodsList = gson.fromJson(customerReturnListGoodsStr, new TypeToken<List<CustomerReturnListGoods>>() {
        }.getType());
        customerReturnList.setUserId(currentUser.getUserId());
        customerReturnList.setReturnNumber(returnNumber);
        customerReturnListDao.insertCustomerReturnList(customerReturnList);
        CustomerReturnList newCustomerReturnList =customerReturnListDao.selectCustomerReturnListByNumber(returnNumber);
        Integer customerReturnListId = newCustomerReturnList.getCustomerReturnListId();

        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            customerReturnListGoods.setCustomerReturnListId(customerReturnListId);
            customerReturnListGoodsDao.insertCustomerReturnListGoods(customerReturnListGoods);
            Goods goods = goodsDao.findByGoodsId(customerReturnListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()+customerReturnListGoods.getGoodsNum());
            goodsDao.updateGoods(goods);
        }
        ServiceVO serviceVO = new ServiceVO(100, "请求成功", null);
        return serviceVO;
    }
    //4.1、客户退货单查询（可查询条件：退货单号、客户、退款状态）
    @Override
    public List<CustomerReturnList> getCustomerReturnList(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<CustomerReturnList> list = customerReturnListDao.selectCustomerReturnList(returnNumber,customerId,state,sTime,eTime);
        return list;
    }
    //4.2、退货单商品信息
    @Override
    public List<CustomerReturnListGoods> getCustomerReturnListGoodsList(Integer customerReturnListId) {
        List<CustomerReturnListGoods> list = customerReturnListGoodsDao.selectCustomerReturnListGoodsList(customerReturnListId);
        return list;
    }
    //4.3、删除退货单
    @Override
    @Transactional
    public ServiceVO deleteCustomerReturnListAndGoods(Integer customerReturnListId) {
        customerReturnListGoodsDao.removeCustomerReturnListGoods(customerReturnListId);
        customerReturnListDao.removeCustomerReturnList(customerReturnListId);
        ServiceVO serviceVO = new ServiceVO(100, "删除成功", null);
        return serviceVO;
    }
    //4.2、客户退货统计（可根据 商品类别、商品编码或名称 条件查询）
    @Override
    public String countCustomerReturnListGoods(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<SaleCountVo> list = customerReturnListDao.countCustomerReturnListGoods(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
    //客户统计 退货单 支付结算
    @Override
    public ServiceVO updateStateCustomerReturnList(String customerReturnListId) {
        customerReturnListDao.updateStateCustomerReturnList(customerReturnListId);

        ServiceVO serviceVO = new ServiceVO(100, "支付结算成功", null);
        return serviceVO;
    }
}
